<script setup lang="ts">
import { getArticleDetailAPI, optArticleAPI, type ArticleRowItem } from '@/apis/article'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { showLoadingToast, showSuccessToast, showFailToast } from 'vant'

// script 中通过 useRoute() 获取路由参数
const route = useRoute()
// 路由 /article/:id 的动态路由参数，通过 params.id 获取
const id = route.params.id

// 文章详情
const article = ref<ArticleRowItem>()
// 获取文章详情
const getArticleDetail = async () => {
  // 根据 id 参数获取对应文章详情
  const res = await getArticleDetailAPI(id as string)
  // 保存文章数据
  article.value = res.data
}

// 组件初始化时，直接调用获取
getArticleDetail()

// 点赞/收藏操作类型常量
const OPT_TYPE = {
  LIKE: 1, // 点赞
  COLLECT: 2, // 收藏
}

// 点赞处理函数
const handleLike = async () => {
  if (!article.value) return

  // 显示加载提示
  showLoadingToast({
    message: '处理中...',
    forbidClick: true,
  })

  try {
    // 调用点赞接口
    await optArticleAPI({
      id: article.value.id,
      optType: OPT_TYPE.LIKE,
    })

    // 切换点赞状态
    article.value.likeFlag = article.value.likeFlag === 1 ? 0 : 1
    // 更新点赞数
    article.value.likeCount =
      article.value.likeFlag === 1 ? article.value.likeCount + 1 : article.value.likeCount - 1

    showSuccessToast(article.value.likeFlag === 1 ? '点赞成功' : '取消点赞')
  } catch (error) {
    showFailToast('操作失败，请重试')
    console.error('点赞操作失败:', error)
  }
}

// 收藏处理函数
const handleCollect = async () => {
  if (!article.value) return

  // 显示加载提示
  showLoadingToast({
    message: '处理中...',
    forbidClick: true,
  })

  try {
    // 调用收藏接口
    await optArticleAPI({
      id: article.value.id,
      optType: OPT_TYPE.COLLECT,
    })

    // 切换收藏状态
    article.value.collectFlag = article.value.collectFlag === 1 ? 0 : 1

    showSuccessToast(article.value.collectFlag === 1 ? '收藏成功' : '取消收藏')
  } catch (error) {
    showFailToast('操作失败，请重试')
    console.error('收藏操作失败:', error)
  }
}
</script>

<template>
  <div class="detail-page">
    <van-nav-bar left-text="返回" left-arrow @click-left="$router.back()" fixed title="面经详情" />
    <header class="header">
      <h1>{{ article?.stem }}</h1>
      <p>
        {{ article?.createdAt }} | {{ article?.views }} 浏览量 | {{ article?.likeCount }} 点赞数
      </p>
      <p>
        <!-- 把 http 图片代理转发到 https 服务中 -->
        <img :src="`https://wsrv.nl/?url=${article?.avatar}`" alt="" />
        <span>{{ article?.creator }}</span>
      </p>
    </header>

    <main class="body" v-html="article?.content"></main>

    <div class="opt">
      <!-- 收藏按钮 - 根据 collectFlag 动态添加 active 类 -->
      <van-icon
        :class="{ active: article?.collectFlag === 1 }"
        name="star-o"
        @click="handleCollect"
      />
      <!-- 点赞按钮 - 根据 likeFlag 动态添加 active 类 -->
      <van-icon :class="{ active: article?.likeFlag === 1 }" name="like-o" @click="handleLike" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>
